//
//  MyCenterViewController.m
//  ForYourBeauty
//
//  Created by Kelly xu on 2019/5/22.
//  Copyright © 2019 changrenTech. All rights reserved.
//

#import "MyCenterViewController.h"
#import "MyCenterListCell.h"
#import "MyCenterDescCell.h"
#import "MyDeviceListViewController.h"
#import "MyFavListViewController.h"
#import "MyReportListViewController.h"
#import "CompleteUserInfoViewController.h"
#import "AppInfoViewController.h"
#import "SettingViewController.h"
#import "InfoListViewController.h"
#import "UserCenterViewModel.h"
#import "GMWKWebViewController.h"
@interface MyCenterViewController ()<UITableViewDataSource,UITableViewDelegate>{
    
}
@property(nonatomic,weak)IBOutlet UIImageView *listBg;
@property(nonatomic,weak)IBOutlet UIView *bgView;
@property(nonatomic,weak)IBOutlet UIButton *userIconBtn;
@property(nonatomic,weak)IBOutlet UILabel *nickNameLab;
@property(nonatomic,weak)IBOutlet UILabel *remarkNameLab;
@property(nonatomic,strong)UITableView *mainTableView;
@property(nonatomic,strong)NSArray *titleArray;
@property(nonatomic,strong)NSArray *iconArray;
@property(nonatomic,strong)UserCenterViewModel *userCenterViewModel;
@end

@implementation MyCenterViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    self.navigationController.navigationBarHidden = YES;
    [self.listBg setImage:[[UIImage imageNamed:@"cornerBg"] stretchableImageWithLeftCapWidth:[UIImage imageNamed:@"cornerBg"].size.width/2 topCapHeight:[UIImage imageNamed:@"cornerBg"].size.height/2]];
    self.bgView.layer.cornerRadius = 10;
    self.bgView.backgroundColor = [UIColor clearColor];
    self.bgView.clipsToBounds = YES;
    self.userIconBtn.layer.cornerRadius = 81.5/2;
    self.userIconBtn.clipsToBounds = YES;
    [self createUI];
}
-(void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:YES];
    [self.userCenterViewModel fetchUserInfo:[LoginTools sharedTools].userId cmp:^(UserInfoModel * _Nonnull userModel) {
        [LoginTools sharedTools].userInfoModel = userModel;
        [self.userIconBtn sd_setImageWithURL:[NSURL URLWithString:userModel.userIcon] forState:UIControlStateNormal];
        self.nickNameLab.text = [NSString stringWithFormat:@"%@",userModel.userNickname];
        self.remarkNameLab.text = [NSString stringWithFormat:@"%@",userModel.userSignature];
    }];
}
-(void)createUI{
    [self.bgView addSubview:self.mainTableView];
    [self.mainTableView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.bgView);
        make.right.equalTo(self.bgView);
        make.top.equalTo(self.bgView);
        make.bottom.equalTo(self.bgView);
    }];
}
#pragma mark - UITableViewDelegate
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return [self.titleArray count]+1;
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    if (indexPath.row<[self.titleArray count]) {
        return 54;
    }else{
        return 44;
    }
}
-(UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    if (indexPath.row<[self.titleArray count]) {
        MyCenterListCell *cell = [tableView dequeueReusableCellWithIdentifier:@"MyCenterListCell" forIndexPath:indexPath];
        cell.titleLab.text = [self.titleArray objectAtIndex:indexPath.row];
        [cell.icon setImage:[UIImage imageNamed:[self.iconArray objectAtIndex:indexPath.row]]];
        return cell;
    }else{
        MyCenterDescCell *cell = [tableView dequeueReusableCellWithIdentifier:@"MyCenterDescCell" forIndexPath:indexPath];
        return cell;
    }
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    switch (indexPath.row) {
        case 0:
        {
            MyDeviceListViewController *device = [[MyDeviceListViewController alloc]init];
            [self.rt_navigationController pushViewController:device animated:YES complete:nil];
        }
            break;
        case 1:
        {
            MyFavListViewController *favList = [[MyFavListViewController alloc]init];
            [self.rt_navigationController pushViewController:favList animated:YES complete:nil];
        }
            break;
        case 2:
        {
            MyReportListViewController *reportList = [[MyReportListViewController alloc]init];
            [self.rt_navigationController pushViewController:reportList animated:YES complete:nil];
        }
            break;
        case 3:
        {
            CompleteUserInfoViewController *completeUserInfo = [[CompleteUserInfoViewController alloc]init];
            completeUserInfo.isFromUserCenter = YES;
            [self.rt_navigationController pushViewController:completeUserInfo animated:YES complete:nil];
        }
            break;
        case 4:
        {
            Toast(@"敬请期待");
        }
            break;
        case 5:
        {
            GMWKWebViewController *webView = [[GMWKWebViewController alloc]init];
            webView.url = @"https://tb.53kf.com/code/app/A67889FD499F556C15F78A820D19647E9/1?device=ios";
            [self.rt_navigationController pushViewController:webView animated:YES complete:nil];
        }
            break;
        case 6:
        {
            AppInfoViewController *appInfo = [[AppInfoViewController alloc]init];
            [self.rt_navigationController pushViewController:appInfo animated:YES complete:nil];
        }
            break;
        default:
            break;
    }
}
#pragma mark - instance
-(UITableView*)mainTableView{
    if (!_mainTableView) {
        _mainTableView = [[UITableView alloc]init];
        _mainTableView.delegate = self;
        _mainTableView.dataSource = self;
        _mainTableView.tableFooterView = [UIView new];
        [_mainTableView registerNib:[UINib nibWithNibName:@"MyCenterListCell" bundle:nil] forCellReuseIdentifier:@"MyCenterListCell"];
        [_mainTableView registerNib:[UINib nibWithNibName:@"MyCenterDescCell" bundle:nil] forCellReuseIdentifier:@"MyCenterDescCell"];
        _mainTableView.showsVerticalScrollIndicator = NO;
        _mainTableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        _mainTableView.scrollEnabled = NO;
    }
    return _mainTableView;
}
-(NSArray*)titleArray{
    if (!_titleArray) {
        _titleArray = @[@"我的设备",@"我的收藏",@"我的报告",@"个人资料",@"使用介绍",@"客服",@"关于我们"];
    }
    return _titleArray;
}
-(NSArray*)iconArray{
    if (!_iconArray) {
        _iconArray = @[@"icon_device",@"icon_fav",@"icon_report",@"perInfo",@"icon_desc",@"icon_kefu",@"aboutUs"];
    }
    return _iconArray;
}
-(UserCenterViewModel*)userCenterViewModel{
    if (!_userCenterViewModel) {
        _userCenterViewModel = [[UserCenterViewModel alloc]init];
    }
    return _userCenterViewModel;
}
#pragma mark - buttonPressed
-(IBAction)settingBtnPressed:(id)sender{
    SettingViewController *setting = [[SettingViewController alloc] init];
    [self.rt_navigationController pushViewController:setting animated:YES complete:nil];
}
-(IBAction)noticeBtnPressed:(id)sender{
    InfoListViewController *infoList = [[InfoListViewController alloc]init];
    [self.rt_navigationController pushViewController:infoList animated:YES complete:nil];
}
-(IBAction)editBtnPressed:(id)sender{
    
}
-(IBAction)userIconBtnPressed:(id)sender{
    
}
/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
